package com.khtn.dao.impl;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.khtn.dao.UserDao;
import com.khtn.model.User;
@Repository
@Transactional(readOnly=true)
public class UserDaoImpl implements UserDao {

	private EntityManager entityManager =null;
	
	@PersistenceContext
	public void setEntityManager(EntityManager entityManager) {
		this.entityManager = entityManager;
	}

	@Transactional(readOnly=false,propagation=Propagation.REQUIRES_NEW)
	public User create(User user) {
		// TODO Auto-generated method stub
		return entityManager.merge(user);
	}

	@Transactional(readOnly=false,propagation=Propagation.REQUIRES_NEW)
	public User update(User user) {
		// TODO Auto-generated method stub
		return entityManager.merge(user);
	}

	@Transactional(readOnly=false,propagation=Propagation.REQUIRES_NEW)
	public void delete(User user) {
		// TODO Auto-generated method stub
		entityManager.remove(entityManager.merge(user));
	}
	public User read(String userName) {
		// TODO Auto-generated method stub
		return entityManager.find(User.class,userName);
	}
	
}
